package com.nwl.crm.service;

import com.nwl.crm.dao.PlatformMapper;
import com.nwl.crm.dto.PlatformDto;
import com.nwl.crm.po.Platform;
import com.nwl.crm.po.PlatformExample;
import com.nwl.crm.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.UUID;

@Service
public class PlatformService extends AbstractService<PlatformDto>{

    @Autowired
    private PlatformMapper mapper;

    public void getAll(PlatformDto dto){
        List<Platform> pos=mapper.selectByExample(null);
        dto.setPos(pos);
    }

    public void layuiList(PlatformDto dto){
        long count = mapper.countBy(dto);
        List<Platform> data = mapper.layuiList(dto);

        dto.getPageUtil().setCount(count);
        dto.getPageUtil().setData(data);
    }

    public void saveOrUpdate(PlatformDto dto){
        if(!StringUtils.isEmpty(dto.getPo())){
            if(StringUtils.isEmpty(dto.getPo().getId())){
                dto.getPo().setId(UUID.randomUUID().toString());
                mapper.insert(dto.getPo());
            }else{
                mapper.updateByPrimaryKey(dto.getPo());
            }
        }
    }

    public void delete(PlatformDto dto){
        String idStr = dto.getIds();
        String[] ids = idStr.split(",");
        PlatformExample example = new PlatformExample();
        example.createCriteria().andIdIn(Arrays.asList(ids));
        mapper.deleteByExample(example);
    }

}
